package com.wenx.v3system.modular.cloud.controller;

import com.wenx.base.rest.BaseRestController;
import com.wenx.v3core.response.R;
import com.wenx.v3secure.annotation.RequiresPermissions;
import com.wenx.v3secure.enums.SystemPermission;
import com.wenx.v3system.modular.cloud.domain.dto.SysTeamDto;
import com.wenx.v3system.modular.cloud.domain.dto.TeamRequestDto;
import com.wenx.v3system.modular.cloud.domain.query.SysTeamQuery;
import com.wenx.v3system.modular.cloud.service.SysTeamRestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

import static com.wenx.v3secure.annotation.RequiresPermissions.Logical.OR;

/**
 * 团队管理Controller
 * 提供团队的CRUD操作和成员管理功能
 *
 * @author wenx
 * @since 1.0.0
 */
@Slf4j
@Tag(name = "团队管理API")
@RestController
@RequestMapping("/api/v3/sys/teams")
public class SysTeamRestController extends BaseRestController<SysTeamDto, SysTeamQuery, SysTeamRestService> {

    public SysTeamRestController(SysTeamRestService service) {
        super(service);
    }

    @Override
    @PostMapping("/page")
    @Operation(summary = "分页查询团队", description = "根据查询条件分页获取团队列表")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R page(@RequestBody SysTeamQuery query) {
        return R.ok(service.page(query));
    }

    /**
     * 获取所有团队（不分页）
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有团队", description = "获取所有团队列表，不分页")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R getAllTeams() {
        return R.ok(service.list());
    }

    @PostMapping("/get/{id}")
    @Operation(summary = "获取团队详情", description = "根据团队ID获取团队详细信息")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R get(@PathVariable Serializable id) {
        return R.ok(service.get(id));
    }

    @Override
    @PostMapping("/add")
    @Operation(summary = "新增团队", description = "创建新的团队")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MANAGE_CODE}, logical = OR)
    public R add(@RequestBody SysTeamDto dto) {
        service.add(dto);
        return R.ok();
    }

    @Override
    @PutMapping("/update")
    @Operation(summary = "更新团队", description = "更新团队信息")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MANAGE_CODE}, logical = OR)
    public R update(@RequestBody SysTeamDto dto) {
        service.update(dto);
        return R.ok();
    }

    @Override
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除团队", description = "根据团队ID删除团队")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MANAGE_CODE}, logical = OR)
    public R delete(@PathVariable Serializable id) {
        service.delete(id);
        return R.ok();
    }

    /**
     * 批量删除团队
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除团队", description = "根据团队ID列表批量删除团队")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MANAGE_CODE}, logical = OR)
    public R batchDelete(@RequestBody TeamRequestDto request) {
        service.batchDelete(request.getIds());
        return R.ok();
    }

    /**
     * 获取用户的团队列表
     */
    @PostMapping("/user/teams")
    @Operation(summary = "获取用户团队", description = "获取指定用户的团队列表")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R getUserTeams(@RequestBody TeamRequestDto request) {
        return R.ok(service.getUserTeams(request.getId()));
    }

    /**
     * 获取用户可加入的团队列表
     */
    @PostMapping("/users/available-teams")
    @Operation(summary = "获取用户可加入的团队", description = "获取用户可加入的团队列表")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R getAvailableTeams(@RequestBody TeamRequestDto request) {
        return R.ok(service.getAvailableTeams(request.getId()));
    }

    /**
     * 获取团队成员列表（分页）
     */
    @PostMapping("/members/page")
    @Operation(summary = "分页获取团队成员", description = "分页获取指定团队的成员列表")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_READ_CODE}, logical = OR)
    public R getTeamMembersPage(@RequestBody TeamRequestDto request) {
        return R.ok(service.getTeamMembersPage(
            request.getTeamId(),
            request.getCurrent(),
            request.getSize(),
            request.getKeyword()
        ));
    }

    /**
     * 添加团队成员
     */
    @PostMapping("/members/add")
    @Operation(summary = "添加团队成员", description = "向团队添加成员")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MEMBER_MANAGE_CODE}, logical = OR)
    public R addTeamMembers(@RequestBody TeamRequestDto request) {
        service.addTeamMembers(request.getTeamId(), request.getUserIds());
        return R.ok();
    }

    /**
     * 移除团队成员
     */
    @DeleteMapping("/members/remove")
    @Operation(summary = "移除团队成员", description = "从团队移除成员")
    @RequiresPermissions(value = {SystemPermission.SYSTEM_ALL_CODE, SystemPermission.TEAM_MEMBER_MANAGE_CODE}, logical = OR)
    public R removeTeamMembers(@RequestBody TeamRequestDto request) {
        service.removeTeamMembers(request.getTeamId(), request.getUserIds());
        return R.ok();
    }

}